package compilers.compiler;

import java.io.FileOutputStream;
import java.io.PrintStream;

import compilers.AST.Tree;
import compilers.exceptions.SemanticalException;
import compilers.exceptions.SyntacticalException;
import compilers.symbolTable.SymbolTable;

/**
 * @author  jens
 */
public class Compiler {

	private Tree fTree;
	private SymbolTable fSymbolTable;
	private SyntacticalException fSyntaxException;
	private SemanticalException fSemanticalException;
	
	public Compiler(Tree tree, SymbolTable symbolTable, SyntacticalException syntaxException, SemanticalException semanticalException) {
		fTree = tree;
		fSymbolTable = symbolTable;
		fSyntaxException = syntaxException;
		fSemanticalException = semanticalException;
	}
	
	public Tree getTree() {
		return fTree;
	}
	
	public SymbolTable getSymbolTable() {
		return fSymbolTable;
	}
	
	public void checkSyntax() throws SyntacticalException {
		if (fSyntaxException != null) {
			throw fSyntaxException;
		}
	}
	
	public void checkSemantics() throws SemanticalException {
		if (fSemanticalException != null) {
			throw fSemanticalException;
		}
		fTree.checkSemantics(fSymbolTable.getRoot());
	}
	
	public void generatePcode(String fileName) throws Exception {
		try {
			PGenerator pGen = new PGenerator(fileName);
			fTree.generatePcode(pGen, fSymbolTable.getRoot());
			pGen.finish();
		} catch (Exception e) {
			throw e;
		}
	}
	
	public void printAST() {
		fTree.print();
	}
	
	public void printSymbolTable() {
		fSymbolTable.print();
	}
}
